package com.ruoyi.storage.orderWarehousing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.basicData.product.domain.Product;
import com.ruoyi.basicData.product.mapper.ProductMapper;
import com.ruoyi.basicData.ruleSetting.domain.AccessoryDataVo;
import com.ruoyi.basicData.ruleSetting.service.IAccessorySupplierRelevanceService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.storage.bicycle.domain.Bicycle;
import com.ruoyi.storage.bicycle.domain.vo.BicycleFittingVo;
import com.ruoyi.storage.bicycle.domain.vo.BicycleVo;
import com.ruoyi.storage.bicycle.mapper.BicycleMapper;
import com.ruoyi.storage.fitting.domain.FittingValue;
import com.ruoyi.storage.fitting.mapper.FittingMapper;
import com.ruoyi.storage.tickets.domain.Tickets;
import com.ruoyi.storage.tickets.mapper.TicketsMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.orderWarehousing.mapper.BicycleOrderMapper;
import com.ruoyi.storage.orderWarehousing.domain.BicycleOrder;
import com.ruoyi.storage.orderWarehousing.service.IBicycleOrderService;

/**
 * 其他入库单Service业务层处理
 * 
 * @author zzx
 * @date 2024-02-06
 */
@Service
public class BicycleOrderServiceImpl implements IBicycleOrderService 
{
    @Autowired
    private BicycleOrderMapper bicycleOrderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TicketsMapper ticketsMapper;
    @Autowired
    private BicycleMapper bicycleMapper;
    @Autowired
    private  FittingMapper fittingMapper;
    @Autowired
    private  IAccessorySupplierRelevanceService accessorySupplierRelevanceService;
    @Autowired
    private  ISysDictDataService sysDictDataService;

    /**
     * 查询其他入库单
     * 
     * @param bicycleId 其他入库单主键
     * @return 其他入库单
     */
    @Override
    public Bicycle selectBicycleOrderByBicycleId(String bicycleId)
    {
        Bicycle bicycle = bicycleMapper.selectBicycleByBicycleId(bicycleId);
        if (bicycle==null){
            bicycle.setBicycleId(bicycleId);
        }
        List<FittingValue> fittingValues=fittingMapper.selectFittingValueByBicycle(bicycleId);
        Map<String, String> map = new HashMap<>();
        for (FittingValue fittingValue:fittingValues) {
            map.put(fittingValue.getFittingFabienName(),fittingValue.getFitting());
        }
        bicycle.setMap(map);
        return bicycle;
    }

    /**
     * 查询其他入库单列表
     * 
     * @param  其他入库单
     * @return 其他入库单
     */
    @Override
    public List<Bicycle> selectBicycleOrderList(Bicycle bicycle)
    {
        List<Bicycle> bicycleOrders = bicycleMapper.selectBicycleOrderList(bicycle);
        for (Bicycle bicycleListVo : bicycleOrders) {
            List<FittingValue> fittingValues=fittingMapper.selectFittingValueByBicycle(bicycleListVo.getBicycleId());
            Map<String, String> map = new HashMap<>();
            for (FittingValue fittingValue:fittingValues) {
                map.put(fittingValue.getFittingFabienName(),fittingValue.getFitting());
            }
            bicycleListVo.setMap(map);
        }
        return bicycleOrders;
    }

    /**
     * 新增其他入库单
     * 
     * @param  其他入库单
     * @return 结果
     */
    @Override
    public AjaxResult insertBicycleOrder(BicycleVo bicycleVo) {
        Bicycle bicycle = new Bicycle();
        BeanUtils.copyProperties(bicycleVo, bicycle);
        //配件信息
        List<AccessoryDataVo> accessoryDataVos = bicycleVo.getAccessoryDataVos();
        if(accessoryDataVos == null || accessoryDataVos.isEmpty()) {
            return AjaxResult.error("提交失败，未进行合法性校验~");
        }
        List<String> noSupplierLabels = accessoryDataVos.stream()
                .filter(accessoryDataVo -> !accessorySupplierRelevanceService.hasSupplier(accessoryDataVo.getAccessoryType(), accessoryDataVo.getData()))
                .map(accessoryDataVo -> sysDictDataService.selectDictLabel("accessory_type", accessoryDataVo.getAccessoryType()))
                .collect(Collectors.toList());
        if(!noSupplierLabels.isEmpty()) {
            String errorMsg = String.join("、", noSupplierLabels) + "不存在供应商";
            return AjaxResult.error(errorMsg);
        }
        bicycle.setType(1L);
        bicycle.setAudit(0L);
        if (bicycle.getBicycleId().length() != 0) {
            Tickets tickets = ticketsMapper.selectTicketsByTicketsId(bicycle.getOrderId());
            bicycle.setColor(tickets.getColour());
        }
        //配件信息
        for (AccessoryDataVo accessoryDataVo : accessoryDataVos) {
            if (!accessoryDataVo.getAccessoryType().equals("controllerId")) {
                FittingValue fittingValue = new FittingValue();
                fittingValue.setFittingId(accessoryDataVo.getFittingId());
                fittingValue.setBicycleId(bicycle.getBicycleId());
                fittingValue.setFitting(accessoryDataVo.getData());
                //添加配件信息
                fittingMapper.insertFittingValue(fittingValue);
            }

        }
        bicycleMapper.insertBicycle(bicycle);
        return AjaxResult.success();
    }

    /**
     * 修改其他入库单
     * 
     * @param bicycleOrder 其他入库单
     * @return 结果
     */
    @Override
    public AjaxResult updateBicycleOrder(BicycleVo bicycleVo)
    {
        Bicycle bicycle = new Bicycle();
        BeanUtils.copyProperties(bicycleVo, bicycle);
        //配件信息
        List<AccessoryDataVo> accessoryDataVos = bicycleVo.getAccessoryDataVos();
        if(accessoryDataVos == null || accessoryDataVos.isEmpty()) {
            return AjaxResult.error("提交失败，未进行合法性校验~");
        }
        List<String> noSupplierLabels = accessoryDataVos.stream()
                .filter(accessoryDataVo -> !accessorySupplierRelevanceService.hasSupplier(accessoryDataVo.getAccessoryType(), accessoryDataVo.getData()))
                .map(accessoryDataVo -> sysDictDataService.selectDictLabel("accessory_type", accessoryDataVo.getAccessoryType()))
                .collect(Collectors.toList());
        if(!noSupplierLabels.isEmpty()) {
            String errorMsg = String.join("、", noSupplierLabels) + "不存在供应商";
            return AjaxResult.error(errorMsg);
        }
        if(bicycleVo.getBicycleId().length()!=0){
            Tickets tickets = ticketsMapper.selectTicketsByTicketsId(bicycleVo.getOrderId());
            bicycleVo.setColor(tickets.getColour());
        }
        for (AccessoryDataVo accessoryDataVo:accessoryDataVos) {
            if(!accessoryDataVo.getAccessoryType().equals("controllerId")){
                FittingValue fittingValue = new FittingValue();
                fittingValue.setFittingId(accessoryDataVo.getFittingId());
                fittingValue.setBicycleId(bicycleVo.getBicycleId());
                fittingValue.setFitting(accessoryDataVo.getData());
                if(fittingMapper.selectFittingExist(fittingValue)){
                    //修改配件信息
                    fittingMapper.updateFittingValue(fittingValue);
                }else {
                    //添加配件信息
                    fittingMapper.insertFittingValue(fittingValue);
                }
            }
        }
        bicycleMapper.updateBicycle(bicycle);
        return AjaxResult.success();
    }

    /**
     * 批量删除其他入库单
     * 
     * @param bicycleIds 需要删除的其他入库单主键
     * @return 结果
     */
    @Override
    public int deleteBicycleOrderByBicycleIds(String[] bicycleIds)
    {
        return bicycleMapper.deleteBicycleByBicycleIds(bicycleIds);
    }

    /**
     * 删除其他入库单信息
     * 
     * @param bicycleId 其他入库单主键
     * @return 结果
     */
    @Override
    public int deleteBicycleOrderByBicycleId(String bicycleId)
    {
        return bicycleMapper.deleteBicycleByBicycleId(bicycleId);
    }

    /**
     * 审核
     * @param bicycleIds
     * @return
     */
    @Override
    public AjaxResult updateAudit(String[] bicycleIds) {
        bicycleMapper.updateBicycleAudit(bicycleIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateUnAudit(String[] bicycleIds) {
        bicycleMapper.updateBicycleUnAudit(bicycleIds);
        return AjaxResult.success();
    }
}
